#include <stdio.h>
#include <sys/queue.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <evhttp.h>
#include "lib.h"
Pvoid_t ja=(PWord_t)NULL;

static void cb_split(struct evhttp_request *r,void *d)
{
	struct evbuffer *evb=evbuffer_new();
	evhttp_add_header(r->output_headers,"Content-Encoding","GBK");
	evhttp_add_header(r->output_headers,"Content-Type","text/html");
	struct evkeyvalq q;
	evhttp_parse_query(r->uri,&q);
	const char *str=evhttp_find_header(&q,"str");
	if(str)
	{
		char *o=gbk_split((unsigned char *)str,0);
		evbuffer_add_printf(evb,"%s",o);
		free(o);
	}
	else
	{
		evbuffer_add_printf(evb,"%s","");
	}
	evhttp_send_reply(r,HTTP_OK,"fine",evb);	
	evbuffer_free(evb);
	struct evkeyval *kv;
	TAILQ_FOREACH(kv,&q,next){
		free(kv->key);
		free(kv->value);
		free(kv);
	}
}

int main(int argc,char **argv)
{
	load_dict();
	load_chinese_stop_word();
	struct event_base *base=event_init();
	struct evhttp *eh=evhttp_new(base);
	int r=evhttp_bind_socket(eh,"10.88.15.195",9009);
	evhttp_set_cb(eh,"/split",cb_split,NULL);
	event_dispatch();
	evhttp_free(eh);
}
